package com.biz.primus.ms.promotion.dao.ro;

import com.biz.primus.base.redis.bean.BaseRedisObject;
import com.biz.primus.base.vo.member.MemberInfoVo;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * 促销模块Ro基类
 *
 * @author david-liu
 */
public abstract class BasePromotionRo<T extends Serializable> extends BaseRedisObject<T> {
    private static final long serialVersionUID = -7528631762277623432L;

    public boolean isMemberInPromotionScope(MemberInfoVo memberInfo) {
        List<Integer> effectMemberLevels = this.getEffectMemberLevelsList();
        List<Long> effectMemberTags = this.getEffectMemberTagsList();
        if (Objects.isNull(memberInfo)) {
            return !CollectionUtils.isNotEmpty(effectMemberLevels) || effectMemberLevels.contains(0);
        } else {
            boolean isMemberLevelInPromotionScope = !CollectionUtils.isNotEmpty(effectMemberLevels) || effectMemberLevels.contains(memberInfo.getMemberLevel());
            boolean isMemberTagInPromotionScope = !CollectionUtils.isNotEmpty(effectMemberTags) || CollectionUtils.intersection(effectMemberTags, memberInfo.getMemberTagIds()).size() == memberInfo.getMemberTagIds().size();
            return isMemberLevelInPromotionScope && isMemberTagInPromotionScope;
        }
    }

    public List<Integer> getEffectMemberLevelsList() {
        return Arrays.stream(StringUtils.split(Optional.ofNullable(this.getEffectMemberLevels()).orElse(""), ",")).filter(s -> "".equals(s) || "null".equals(s)).map(Integer::valueOf).collect(Collectors.toList());
    }

    public List<Long> getEffectMemberTagsList() {
        return Arrays.stream(StringUtils.split(Optional.ofNullable(this.getEffectMemberTags()).orElse(""), ",")).filter(s -> "".equals(s) || "null".equals(s)).map(Long::valueOf).collect(Collectors.toList());
    }

    /**
     * 获取促销生效的会员等级
     *
     * @return 会员等级字符串(以逗号分割)
     */
    public abstract String getEffectMemberLevels();

    /**
     * 获取促销生效的会员标签
     *
     * @return 会员标签字符串(以逗号分割)
     */
    public abstract String getEffectMemberTags();
}
